package com.ebelter.bodyfatscale.util;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.format.DateUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import com.ebelter.bodyfatscale.ApplicationHelper;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.util.Date;
import java.util.Locale;

/**
 * 特殊文本
 * Created by pan on 2017/6/17.
 */
public class SpecialText {

    /**
     * 带粗体的字符串
     *
     * @param text
     * @param words
     * @param textColor
     * @param folds
     * @return
     */
    public static Spannable severalBold(String text, String[] words, int textColor, float folds) {
        if (text == null) {
            return null;
        }
        Spannable span = new SpannableString(text);
        for (String word : words) {
            int start = text.indexOf(word);
            span.setSpan(new RelativeSizeSpan(folds), start, start + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        span.setSpan(new ForegroundColorSpan(textColor), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }

    public static Spannable setBold(String text, String word, int textColor, float folds) {
        if (text == null) {
            return null;
        }
        Spannable span = new SpannableString(text);
        int start = text.indexOf(word);
        span.setSpan(new RelativeSizeSpan(folds), start, start + word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(textColor), 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }



    public static void showSpan(TextView tv, float value, String word, int color) {
        if (tv == null) {
            return;
        }
        String text = SpecialText.numberLocle_1(tv.getContext(), value) + word;
        ;
        Spannable weight = SpecialText.setBold(text, word, color, 0.7f);
        tv.setText(weight);
    }

    public static void showSpan(TextView tv, float value, String word, int color, float scales) {
        if (tv == null) {
            return;
        }
        String text = SpecialText.numberLocle_1(tv.getContext(), value) + word;
        ;
        Spannable weight = SpecialText.setBold(text, word, color, scales);
        tv.setText(weight);
    }







    /**
     * 日期国际化显示的字符串
     *
     * @param ctx
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String internationalDate(Context ctx, int year, int month, int day) {
        if (ctx == null) {
            return "";
        }
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, ctx.getResources().getConfiguration().locale);
        return df.format(new Date(year - 1900, month - 1, day, 0, 0, 0));
    }

    public static String internationalDateMedium(Context ctx, int year, int month, int day) {
        if (ctx == null) {
            return "";
        }
        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, ctx.getResources().getConfiguration().locale);
        return df.format(new Date(year - 1900, month - 1, day, 0, 0, 0));
    }

    //时间格式化
    public static String internationalDateShort(Context ctx, int year, int month, int day) {
        if (ctx == null) {
            return "";
        }
        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ctx.getResources().getConfiguration().locale);
        return df.format(new Date(year - 1900, month - 1, day, 0, 0, 0));
    }
    //时间格式化
    public static String internationalDatedetail(Context ctx, int year, int month, int day, int hour, int minute, int second) {
        if (ctx == null) {
            return "";
        }
        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, ctx.getResources().getConfiguration().locale);
        return df.format(new Date(year - 1900, month - 1, day, hour, minute, second));
    }

    /**
     * 获取本地化月份数组
     *
     * @param locale
     * @return
     */
    public static String[] getMonths(Locale locale) {
        return new DateFormatSymbols(locale).getMonths();
    }

    /**
     * 获取本地化星期数组
     *
     * @param locale
     * @return
     */
    public static String[] getWeekDasys(Locale locale) {
        return new DateFormatSymbols(locale).getWeekdays();
    }

    /**
     * 获取本地化月份简写数组
     *
     * @param locale
     * @return
     */
    public static String[] getShortMonths(Locale locale) {
        return new DateFormatSymbols(locale).getShortMonths();
    }

    /**
     * 获取本地化星期简写数组
     *
     * @param locale
     * @return
     */
    public static String[] getShortWeekDasys(Locale locale) {
        return new DateFormatSymbols(locale).getShortWeekdays();
    }


    /**
     * 数字本地化的方法
     *
     * @param ctx
     * @param value
     * @return
     */
    public static String numberLocleComma(Context ctx, int value) {
        return String.format(ctx.getResources().getConfiguration().locale, "%,d", value);
    }

    public static String numberLocle(Context ctx, int value) {
        return String.format(ctx.getResources().getConfiguration().locale, "%d", value);
    }

    /**
     * 数字本地化的方法,补0
     *
     * @param ctx
     * @param value
     * @return
     */
    public static String numberZerofillLocle(Context ctx, int value) {
        return String.format(ctx.getResources().getConfiguration().locale, "%02d", value);
    }

    /**
     * 数字本地化的方法
     *
     * @param ctx
     * @param value
     * @return
     */
    public static String numberLocle_1(Context ctx, float value) {
        return String.format(ctx.getResources().getConfiguration().locale, "%.1f", value);
    }
    public static String numberLocle_i(Context ctx, int value) {
        return String.format(ctx.getResources().getConfiguration().locale,"%d", value);
    }

    public static String formatDateStampString0(long when) {
        int format_flags = DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_SHOW_YEAR;
        return DateUtils.formatDateTime(ApplicationHelper.mApplicationContecx, when, format_flags);
    }

    public static String formatDateStampString1(long when) {
        int format_flags = DateUtils.FORMAT_SHOW_DATE| DateUtils.FORMAT_NO_YEAR;
        return DateUtils.formatDateTime(ApplicationHelper.mApplicationContecx, when, format_flags);
    }
    public static String formatDateStampString2(long when) {
        int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_TIME;
        return DateUtils.formatDateTime(ApplicationHelper.mApplicationContecx, when, format_flags);
    }
    public static String formatDateStampString3(long when) {
        int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT
                | DateUtils.FORMAT_ABBREV_ALL
                | DateUtils.FORMAT_SHOW_TIME
                | DateUtils.FORMAT_SHOW_DATE
                | DateUtils.FORMAT_SHOW_YEAR;
        return DateUtils.formatDateTime(ApplicationHelper.mApplicationContecx, when, format_flags);
    }
}
